package com.liangzhong.proxy.cour
{
	import com.liangzhong.commandconst.club.SMDClub;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.court.SMDCourt;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.court.CourtDataManager;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.court.CourtProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	public class CourtProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "CourProxy_proxyName";
		/**
		 * 
		 */
		private var _courtDataManger:CourtDataManager;
		public function CourtProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
			_courtDataManger = AppData.getInstance().courtDataManger;
		}
		/**
		 *接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MAINDEAL:
					return onDeal(subCmdID,buffer,dataSize);
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDCourt.COURT_UP:
					getCourtUp(buffer);
					return true;
			}
			return false;
		}

		/**
		 *请求球场信息  
		 * 
		 */
		public function requestCourtInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.COURT_INFO;
			ProxyUtils.instance.webClient(getCourtInfo,variables,false);
		}
		/**
		 *获得球场信息 
		 * @param $data
		 * 
		 */
		private function getCourtInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,"getCourtInfo");
			if(obj.retcode == 0)
			{
				_courtDataManger.analyaisCourtInfo(obj);
				this.sendNotification(CourtProxyConstant.GET_COURT_INFO);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求球场升级  
		 * 
		 */
		public function requestCourtUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MAINDEAL,SMDCourt.COURT_UP);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回球场升级 
		 * 
		 */
		private function getCourtUp(buffer:ByteArray):void
		{
			
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				_courtDataManger.courtUp(buffer);
				this.sendNotification(CourtProxyConstant.GET_COURT_UP,{status:true});
				Alert.showErrorTip("升级成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(CourtProxyConstant.GET_COURT_UP,{status:false});
			}
		}
		/**
		 *请求冷却时间 
		 * @param $obj
		 * 
		 */
		public function requestClearUpTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,MainCommandType.MAINDEAL,CMD_Logic.DROP_BUILDING_Time);
			sendBuf.writeByte(BuildingType.STADIUM);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
//		/**
//		 *返回冷却时间 
//		 * 
//		 */
//		public function getClearUpTime($obj:Object):void
//		{
//			Alert.showErrorTip("时间冷却成功");
//			_courtDataManger.courtInfoVo.pretime = 0;
//			
//			//			this.sendNotification(PlayerCenterConstant.GET_CLEAR_UP_TIME);
//		}
	}
}